package gins

import (
	"context"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/https"
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/results"
	"github.com/gin-gonic/gin"
	httptransport "github.com/go-kit/kit/transport/http"
	"net/http"
)

func NewJsonResponseDecoder(ginCtx *gin.Context) httptransport.EncodeResponseFunc {
	return func(ctx context.Context, writer http.ResponseWriter, resp interface{}) error {
		err, ok := resp.(error)
		if ok && err != nil {
			result := results.Err(err)
			ginCtx.JSON(https.StatusInternalServerError, result)
			return nil
		}
		ginCtx.JSON(200, results.Suc(resp))
		return nil
	}
}
